#!/usr/bin/env sh

unset LS_COLORS
unset EDITOR

# get this game's configuration
[ -f "$GSH_CONFIG"/config.sh ] && . "$GSH_CONFIG"/config.sh

export PATH=$PATH:$GSH_BIN

# source definition of `gsh` command
# shellcheck source=./lib/gameshell.sh
. "$GSH_LIB/gsh.sh"

export PS1='[mission $(gsh pcm)] $ '
export PS2="> "
export PS3=""
export PS4="+ "

# export the COLUMNS variable so the reflow script can get the terminal size
export COLUMNS

# choose appropriate pager
unset PAGER
tempfile=$(mktemp)
echo "TEST" | "$GSH_ROOT/scripts/pager" 2>"$tempfile" >/dev/null
if [ -s "$tempfile" ]
then
  export PAGER=more
fi
rm -f "$tempfile"

# try to prevent using sudo and similar
su() {
    echo "gsh: su: command not found" >&2
}

sudo() {
    echo "gsh: sudo: command not found" >&2
}

doas() {
    echo "gsh: doas: command not found" >&2
}

# remove quotes from filenames with special characters
# NOTE: --literal doesn't exist in freebsd
if ls --literal / &> /dev/null
then
    alias ls="ls --literal"
fi

# source additional dynamic rc files
if [ -n "$(find "$GSH_CONFIG" -maxdepth 1 -name 'gshrc_*.sh' | head -n1)" ]
then
  for file in "$GSH_CONFIG"/gshrc_*.sh
  do
      # shellcheck source=/dev/null
      . "$file"
  done
fi

if [ -n "$(find "$GSH_CONFIG" -maxdepth 1 -name 'treasure_*.sh' | head -n1)" ]
then
  for file in "$GSH_CONFIG"/treasure_*.sh
  do
      # shellcheck source=/dev/null
      . "$file"
  done
fi

unset file MISSION_DIR

# print message when player wanders outside GameShell filesystem
_check_pwd() {
  if [ "$GSH_MODE" = "DEBUG" ]
  then
    return
  fi

  case "$(pwd -P)" in
    "$GSH_HOME"*)
      :
      ;;
    *)
      echo
      color_echo yellow "$(gettext "You left GameShell's directory structure. Use
    \$ cd
to go back to the GameShell's starting directory.")" >&2
      echo
  esac
}

# display a small message so players know how to get help about gameshell
_help_hint() {
  [ -n "$GSH_QUIET_INTRO" ] || [ "$GSH_HELP_HINT" = "never" ] && return

  if [ "$GSH_HELP_HINT" != "always" ] &&
    { [ "$(gsh pcm)" -gt 3 ] || [ "$GSH_MODE" = "DEBUG" ] || [ "$GSH_HELP_HINT" = "0" ] ; }
  then
    unset GSH_HELP_HINT
    return
  fi
  echo
  echo "[$(gettext "use 'gsh help' to get a list of available commands")]"
}

[ "$GSH_MODE" != "DEBUG" ] && ! [ -d "$GSH_ROOT/.git" ] && gsh protect
